import { batchDelete, getWindowList } from '@/services/window';
import {DeleteOutlined, EditOutlined, PlusOutlined} from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { history } from 'umi';
import { Button, message, Popconfirm } from 'antd';
import React, { useRef, useState } from 'react';
const TableList: React.FC = () => {
  /**
   *  Delete node
   * @zh-CN 删除多个节点
   *
   * @param selectedRows
   */
  const handleRemove = async (selectedRows) => {
    if (!selectedRows) return true;

    try {
      const res = await batchDelete(selectedRows.map((row) => row.id));
      if (res.code == '0') {
        if (actionRef.current) {
          actionRef.current.reload();
          actionRef.current.clearSelected();
        }
        message.success('已删除！');
        return true;
      }
    } catch (error) {
      message.error('删除失败！');
      return false;
    }
  };

  const actionRef = useRef<ActionType>();
  const [currentRow, setCurrentRow] = useState<any>();
  const [selectedRows, setSelectedRows] = useState<any[]>();

  const jumpPublish = (record: any) => {
    if (record.type == 'couplet') {
      history.push({
        pathname:'/windowSetting/buildCouplet',
        state: {
          formValues: record,
      }
      });

    } else {
      history.push({
        pathname:'/windowSetting/buildFloat',
        state: {
          formValues: record,
      }
      });

    }
  };
  const columns: ProColumns<any>[] = [
    {
      title: '专栏描述',
      align: 'center',
      width: '25%',
      dataIndex: 'des',
    },
    {
      title: '宽',
      align: 'center',
      hideInSearch: true,
      dataIndex: 'width',
    },
    {
      title: '高',
      align: 'center',
      hideInSearch: true,
      dataIndex: 'height',
    },
    {
      title: '专栏排期',
      align: 'center',
      hideInSearch: true,
      render: (_, record) => {
        return (
          <div>
            {record.playStart} - {record.playEnd}
          </div>
        );
      },
    },
    {
      title: '显示',
      align: 'center',
      hideInSearch: true,
      dataIndex: 'display',
      valueEnum: {
        '0': {
          text: '否',
        },
        '1': {
          text: '是',
        },
      },
    },
    {
      title: '类型',
      align: 'center',
      // hideInSearch: true,
      dataIndex: 'type',
      valueEnum: {
        couplet: {
          text: '对联',
        },
        bay: {
          text: '飘窗',
        },
      },
    },
    {
      title: '当前状态',
      align: 'center',
      // hideInSearch: true,
      dataIndex: 'stat',
      valueEnum: {
        active: {
          text: '活动中',
          status: 'Success',
        },
        inactive: {
          text: '已失效',
          status: 'Default',
        },
        suspend: {
          text: '待发布',
          status: 'Processing',
        },
      },
    },
    {
      title: '排序号',
      align: 'center',
      hideInSearch: true,
      dataIndex: 'pxh',
    },
    {
      title: '操作',
      align: 'left',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => [
        <a key="edit" onClick={() => jumpPublish(record)}>
          <Button type={"primary"} shape={'round'} icon={<EditOutlined />}>
            编辑
          </Button>
        </a>,
        <Popconfirm title="确定是否删除?" onConfirm={() => handleRemove([record])} key="delete">
          <a style={{ color: 'red' }}>
            <Button danger className={'buttonMargin'} shape={'round'} icon={<DeleteOutlined />}>
              删除
            </Button>
          </a>
        </Popconfirm>,
      ],
    },
  ];

  return (
    <PageContainer
      header={{
        title: false,
      }}
    >
      <ProTable
        // headerTitle="用户列表"
        actionRef={actionRef}
        rowKey={'id'}
        // search={false}
        tableStyle={{ textAlign: 'center' }}
        options={false}
        toolBarRender={() => [
          <Button
            type="primary"
            key="couplet"
            onClick={() => {
              history.push('/windowSetting/buildCouplet');
            }}
          >
            <PlusOutlined />
            对联专栏
          </Button>,
          <Button
            type="primary"
            key="float"
            onClick={() => {
              history.push('/windowSetting/buildFloat');
            }}
          >
            <PlusOutlined />
            漂浮专栏
          </Button>,
          <Button
            key="danger"
            onClick={() => {
              handleRemove(selectedRows);
            }}
          >
            批量删除
          </Button>,
        ]}
        request={async (params, sort, filter) => {
          params.page = params.current;
          // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
          const msg = await getWindowList(params);
          return {
            data: msg.data.list,
            // success 请返回 true，
            // 不然 table 会停止解析数据，即使有数据
            success: msg.code === '0',
            // 不传会使用 data 的长度，如果是分页一定要传
            total: msg.data.total,
          };
        }}
        pagination={{ pageSize: 10 }}
        columns={columns}
        rowSelection={{
          onChange: (_, selectedRows) => {
            setSelectedRows(selectedRows);
          },
        }}
      />
    </PageContainer>
  );
};

export default TableList;
